import { Modal } from 'antd';
import TRNotification from '#/utils/notification';
import React from 'react';
import styles from './index.less';
import { ExclamationCircleOutlined } from '#/utils/antdIcons';

// class AlertComponent extends Component {
//   constructor(props) {
//     super(props);
//     this.state = {
//       visible: false,
//     };
//   }
//   componentDidMount() {
//     this.setState({
//       visible: true,
//     });
//   }
//   render() {
//     const { msg, onPress } = this.props;
//     return (
//       <div className={styles.modaldq}>
//         <Modal
//           bodyStyle={{}}
//           okText="确定"
//           cancelText="取消"
//           visible={this.state.visible}
//           onCancel={() => {
//             this.setState(
//               {
//                 visible: false,
//               },
//               isok => {
//                 onPress(0);
//               },
//             );
//           }}
//           onOk={() => {
//             this.setState(
//               {
//                 visible: false,
//               },
//               isok => {
//                 onPress(1);
//               },
//             );
//           }}
//         >
//           <div className={styles.row}>
//             <ExclamationCircleOutlined
//               style={{ fontSize: '16px', color: '#F58D29', marginRight: 10, marginTop: 3 }}
//             />
//             <div className={styles.content}>{msg}</div>
//           </div>
//         </Modal>
//       </div>
//     );
//   }
// }

const msgConent = function (msg) {
  let arr = msg.split('*');
  if (arr.length === 1) {
    return msg;
  }
  return (
    <div>
      {arr.map((item, index) => (
        <div key={index}>{item}</div>
      ))}
    </div>
  );
};

const warningMsgConent = function (msg) {
  let arr = msg.split('*');
  if (arr.length === 1) {
    return msg;
  }
  return (
    <div className={styles.warningMsgConent}>
      {arr.map((item, index) => (
        <div key={index}>{item}</div>
      ))}
    </div>
  );
};

class AlertModal {
  __key__ = '';
  __modal__ = undefined;
  show = (msg, containerPosition = document.body) => {
    return new Promise((resolve) => {
      if (this.__key__ !== '') {
        return;
      }
      this.__key__ = String(Date.now());
      const that = this;
      Modal.confirm({
        title: msgConent(msg),
        centered: true,
        getContainer: containerPosition,
        okText: '确定',
        cancelText: '取消',
        icon: <ExclamationCircleOutlined />,
        onOk() {
          resolve({ index: 1 });
          that.__key__ = '';
        },
        onCancel() {
          resolve({ index: 0 });
          that.__key__ = '';
        },
      });
    });
  };
  warning = (msg, containerPosition = document.body) => {
    return new Promise((resolve) => {
      Modal.warning({
        centered: true,
        okText: '确认',
        getContainer: containerPosition,
        title: warningMsgConent(msg),
        onOk() {
          resolve({ index: 1 });
        },
      });
    });
  };
  Success = (msg) => {
    Modal.success({
      centered: true,
      okText: '确认',
      title: msgConent(msg),
    });
  };
  Info = (msg) => {
    Modal.info({
      centered: true,
      okText: '确认',
      title: msgConent(msg),
    });
  };
  longWarning = (msg, message, containerPosition = document.body) => {
    return new Promise((resolve) => {
      Modal.warning({
        centered: true,
        okText: '确认',
        title: msg,
        getContainer: containerPosition,
        content: msgConent(message),
        onOk() {
          resolve({ index: 1 });
        },
      });
    });
  };

  error = (msg, message) => {
    var obj = {};
    if (message) {
      obj.content = '失败原因：' + message;
    }
    Modal.error({
      centered: true,
      okText: '确认',
      title: msgConent(msg),
      ...obj,
    });
  };

  eamDelete = (title, content, containerPosition = document.body) => {
    return new Promise((resolve) => {
      if (this.__key__ !== '') {
        return;
      }
      this.__key__ = String(Date.now());
      const that = this;
      this.__modal__ = Modal.confirm({
        className: 'tabledelet',
        title: msgConent(title),
        content,
        centered: true,
        getContainer: containerPosition,
        okText: '确定',
        cancelText: '取消',
        icon: <ExclamationCircleOutlined />,
        zIndex: 1100,
        onOk() {
          resolve({ index: 1 });
          that.__modal__ = undefined;
          that.__key__ = '';
        },
        onCancel() {
          resolve({ index: 0 });
          that.__key__ = '';
          that.__modal__ = undefined;
        },
      });
    });
  };
  eamUpdateDelete = (title, content) => {
    if (this.__modal__) {
      this.__modal__.update({
        title: msgConent(title),
        content,
      });
    }
  };
  dismiss = () => {
    if (this.__key__.length > 0) {
      TRNotification.remove(this.__key__);
      this.__key__ = '';
    }
    if (!this.__modal__) {
      TRNotification.remove(this.__modal__);
      this.__modal__ = undefined;
    }
  };
}

const tAlert = new AlertModal();
export default tAlert;
